#!/bin/bash

set -e
set -x

# wget https://github.com/prometheus/alertmanager/releases/download/v0.28.0/alertmanager-0.28.0.linux-amd64.tar.gz
bypy download alertmanager-0.28.0.linux-amd64.tar.gz
tar xf alertmanager-0.28.0.linux-amd64.tar.gz
mv alertmanager-0.28.0.linux-amd64 /opt/alertmanager
chown -R prometheus:prometheus /opt/alertmanager/
cp alertmanager.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable alertmanager --now
sh ../check_port.sh 9093
time=$(date +'%Y_%m_%d_%H_%M_%S')
sed -i.$time '/9093/s/# - alertmanager/- localhost/' /opt/prometheus/prometheus.yml
sleep 1
time=$(date +'%Y_%m_%d_%H_%M_%S')
sed -i.$time '$r alertmanager.tpl' /opt/prometheus/prometheus.yml
curl -X POST http://localhost:9090/-/reload

set +x
set +e